package raccoon.suvisa.presentacion.modelo.servlets;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.Session;
import raccoon.suvisa.datos.entities.Usuario;
import raccoon.suvisa.datos.fachadas.FachadaDatosRemote;
import raccoon.suvisa.presentacion.Localizador.Localizador;

import javax.servlet.http.*;

/**
 *
 * @author Administrator
 */
public class mlogin extends HttpServlet {
    private FachadaDatosRemote fdb;
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet mlogin</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet mlogin at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        boolean aux=false;
        Usuario u1=new Usuario();
        try {
            fdb = Localizador.encontrarLogica();
            Usuario u=(Usuario)request.getAttribute("usuario");
            String id=new String();
            aux=this.fdb.getMetodosUsuario().validarUserName(u.getUsername(), u.getPassword());
            if(aux)
            {
                id=this.fdb.getMetodosUsuario().retornarUsuario(u.getUsername(), u.getPassword());
                u1=this.fdb.getMetodosUsuario().getUsuario(new BigDecimal(id));
                request.setAttribute("nombre", u1.getNombre());
                request.setAttribute("apellido", u1.getApellido());
                request.setAttribute("rol", u1.getRol());
                request.setAttribute("usuario", u1.getUsername());
                request.setAttribute("contrasena", u1.getPassword());
            }
            System.out.println("Paso!!");
        } catch (Exception ex) {
            System.out.println("No Paso!!");
            ex.printStackTrace();
        }

        if(aux==true)
        {
            HttpSession session=request.getSession(true);
            session.setAttribute("usuario", u1.getUsername());
            session.setAttribute("nombre", u1.getNombre());
            session.setAttribute("apellido", u1.getApellido());
            session.setAttribute("rol", u1.getRol());
            session.setAttribute("ip",  request.getRemoteAddr());
            Date dt=new Date();
            session.setAttribute("fecha",   dt.toString());
                RequestDispatcher rd = request.getRequestDispatcher("suvisa.jsp");
                rd.forward(request, response);
        }
        else
        {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<HTML>");
            out.println("<HEAD>");
            out.println("<TITLE>suVisa - Acceso Denegado</TITLE>");
            out.println("</HEAD>");
            out.println("<BODY>");
            out.println("<P>ACCESO DENEGADO</P>");
            out.println("<a href='login.jsp' title='Inicio' target='_self'>Inicio </a>");
            out.println("</BODY>");
            out.println("</HTML>");
        }
        
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
